package com.jiang.books.domain;

import org.hibernate.annotations.Type;
import org.hibernate.envers.Audited;
import org.joda.time.DateTime;
import org.springframework.data.domain.Auditable;
import static javax.persistence.GenerationType.SEQUENCE;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "jiang_user_group")
@NamedQueries({
        @NamedQuery(name="UserGroup.findAll",
                query="select c from UserGroup c"),
        @NamedQuery(name="UserGroup.findById",
                query="select distinct c from UserGroup c where c.id = :id")
})
@SqlResultSetMapping(
        name="UserGroupResult",
        entities=@EntityResult(entityClass=UserGroup.class)
)
@Audited
public class UserGroup implements Serializable, Auditable<String, Long> {

    private static final long serialVersionUID = 1L;

	private Long id;
	private UserGroup parent;
    private String groupName;
	private String groupDescription;

    // Audit fields
    private String createdBy;
    private DateTime createdDate;
    private String lastModifiedBy;
    private DateTime lastModifiedDate;

    public UserGroup() {
	}

	public UserGroup(UserGroup parent, String groupName, String groupDescription) {
        this.parent = parent;
        this.groupName = groupName;
		this.groupDescription = groupDescription;
	}

    @Id
    @GeneratedValue(strategy = SEQUENCE, generator="JIANG_SEQ")
    @SequenceGenerator(name="JIANG_SEQ", sequenceName="jiang_id_sequence")
    @Column(name = "ID", unique = true, nullable = false)
    public Long getId() {
        return this.id;
    }

    @ManyToOne
    @JoinColumn(name = "parent_id")
    public UserGroup getParent() {
        return parent;
    }

    @Column(name = "group_name")
    public String getGroupName() {
        return groupName;
    }

    @Column(name = "group_description")
    public String getGroupDescription() {
        return groupDescription;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setParent(UserGroup parent) {
        this.parent = parent;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public void setGroupDescription(String groupDescription) {
        this.groupDescription = groupDescription;
    }

    @Override
    public String toString() {
        return "UserGroup{" +
                "id=" + id +
                ", parent=" + parent +
                ", groupName=" + groupName  +
                ", groupDescription=" + groupDescription  +
                "}";
    }

    @Column(name="CREATED_BY")
    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    @Column(name="CREATED_DATE")
    @Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
    public DateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(DateTime createdDate) {
        this.createdDate = createdDate;
    }

    @Column(name="LAST_MODIFIED_BY")
    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    @Column(name="LAST_MODIFIED_DATE")
    @Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
    public DateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(DateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    @Transient
    public boolean isNew() {
        if (id == null) {
            return true;
        } else {
            return false;
        }
    }
}